#!/usr/bin/env python
# coding=utf8


import sys
import os
sys.path.append('..')
sys.path.append('../../')
from i_util.pybeanstalk import PyBeanstalk
import json
import traceback

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.transport.TTransport import TMemoryBuffer
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from bdp.i_crawler.i_extractor.ttypes import BaseInfo, CrawlInfo, ExtractInfo, PageParseInfo
import time
from pymongo import MongoClient
import hashlib
import logging

log_file = os.getcwd()[:os.getcwd().rfind('/')] + '/logs/zhixin.log'
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename=log_file,
                    filemode='w')


def main(min_time, max_time):
    host = '101.201.196.82'
    port = 27017
    database = 'public_data'
    coll = 'zhixing_info'
    client = MongoClient(host, port)
    db = client[database][coll]

    param = {"in_time": {"$gt": min_time, "$lt": max_time}}
    cursor = db.find(param)
    num = 0

    try:
        for item in cursor:
            if num >= 0:

                src_url = item.get("src_url", "")

                data = get_param_data(item)

                data = json.dumps(data)
                site_id = get_md5_i64(item.get("site_id", ""))
                site = src_url.split('//')[-1].split('/')[0]
                base_info = BaseInfo(url=src_url, site_id=site_id, site=site)
                extract_info = ExtractInfo(ex_status=2, extract_data=data, topic_id=42)
                tube = "extract_info"
                send_msg(base_info, extract_info, tube)
            num += 1

    except:
        logging.error(traceback.format_exc())

    logging.info("finish sumnum:%s" % num)


def get_param_data(item):
    '''获取入参'''
    exec_money = item.get("execMoney", "")
    province = item.get("province", "")
    case_state = item.get("caseState", "")
    case_id = item.get("caseCode", "")
    court = item.get("execCourtName", "")
    case_date = item.get("caseCreateTime", "")
    i_name = item.get("pname", "")
    party_card_num = item.get("partyCardNum", "")

    data = {
        "exec_money": exec_money,
        "province": province,
        "case_id": case_id,
        "court": court,
        "case_date": case_date,
        "i_name": i_name,
        "party_card_num": party_card_num,

    }
    return data


import ctypes


def get_md5_i64(obj):
    m = hashlib.md5()
    m.update(obj)
    return ctypes.c_int64(int(m.hexdigest()[8:-8], 16)).value


def send_msg(base_info, extract_info, tube):
    '''往消息队列中发送消息'''
    crawl_info = CrawlInfo()

    pybeanstalk = PyBeanstalk('101.201.102.37')

    req = PageParseInfo(base_info=base_info, crawl_info=crawl_info, extract_info=extract_info, scheduler="a",
                        parse_extends="b", data_extends="c")
    tMemory_b = TMemoryBuffer()
    tBinaryProtocol_b = TBinaryProtocol(tMemory_b)
    req.write(tBinaryProtocol_b)
    body = tMemory_b.getvalue()
    try:
        pybeanstalk.put(tube, body)
    except:
        logging.error(traceback.format_exc())


if __name__ == "__main__":
    min_time = sys.argv[1]
    max_time = sys.argv[2]
    logging.info("zhixing_start\tmin_time:%s\tmax_time:%s\t" % (min_time, max_time))
    main(min_time, max_time)
